package zut.cs.liziflower.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import zut.cs.liziflower.entity.Cart;
import zut.cs.liziflower.entity.Flower;
import zut.cs.liziflower.entity.Result;
import zut.cs.liziflower.mapper.CartMapper;
import zut.cs.liziflower.service.impl.CartServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xuwenke
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Resource
    private CartServiceImpl cartService;
    @Resource
    private CartMapper cartMapper;
    int yes = -1;

    @PutMapping("/add-cart")
    public Result<?> addCart(@RequestParam("userid") Integer userid,
                             @RequestParam("flowerid") Integer flowerid) {
        //首先先根据 userid 和 flowerid 查询购物车是否已存在当前商品 cart
        //如已存在，在 cart.number++,并更新数据库
        //如果不存在，则插入 该商品 number 默认为 1
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        int yes = -1;//判断插入成功
        //查询
        Cart cart = cartService.getByUseridAndFlowerid(userid, flowerid);
        if (cart != null) {//已存在，更新
            cart.setNumber(cart.getNumber() + 1);
            wrapper.eq("userid", userid)
                    .and(q -> q.eq("flowerid", flowerid))
                    .and(p -> p.eq("cartid", cart.getCartid()));
            boolean done = cartService.update(cart, wrapper);
            if (done == true) {
                return Result.success(done);
            } else {
                return Result.error("405", "添加失败", done);
            }
        } else {//未存在，插入
            Cart cart1 = new Cart();
            cart1.setNumber(1);
            cart1.setFlowerid(flowerid);
            cart1.setUserid(userid);
            yes = cartMapper.insert(cart1);
            if (yes != -1) {//插入成功
                return Result.success(yes);
            } else {
                return Result.error("405", "添加失败", null);
            }

        }
    }


    @GetMapping("get-cart")
    public Result<?> getCart(@RequestParam("userid") Integer userid) {
        //获取该用户的所有购物车数据
        //cart表和flower表连接查询，根据userid 和 flowerid 再连接 consumer表
        List<Flower> list = cartService.getCart(userid);
        if (list != null) {
            return Result.success(list);
        }
        return Result.success("购物车为空");
    }

    @DeleteMapping("delete-cart")
    public Result<?> deleteCart(@RequestParam("userid") Integer userid,
                                @RequestParam("flowerid") Integer flowerid) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("userid", userid)
                .and(q -> q.eq("flowerid", flowerid));
        yes = cartMapper.delete(wrapper);
        if (yes != -1) {
            return Result.success("删除成功");
        } else {
            return Result.error("405", "删除失败", null);
        }
    }

    @GetMapping("/get-page-cart")
    public Result<?> getPageCart(@RequestParam Integer userid,
                                 @RequestParam Integer page,
                                 @RequestParam Integer size) {
        List<Flower> list = cartService.getPage(userid, (page - 1) * size, size);
        return Result.success(list);
    }

    @PutMapping("/change-number")
    public Result<?> changeNumber(@RequestBody Cart cart) {
//        System.out.println(cart);

        //更新userid和flowerid处的number
        int flag = cartService.changeNumber(cart.getUserid(), cart.getFlowerid(), cart.getNumber());
        if (flag == 1) {
            return Result.success("成功");
        }
        System.out.println(flag);
        return Result.error("405", "失败", null);
    }


}
